﻿--HEADER Original Position Indexes Changes
IF EXISTS (SELECT * FROM sys.foreign_keys WHERE parent_object_id = object_id('TradingData.[OriginalPositionValidationData]') and name = 'FK_OriginalPositionValidationData_ToOriginalPosition')
ALTER TABLE [TradingData].[OriginalPositionValidationData] DROP CONSTRAINT [FK_OriginalPositionValidationData_ToOriginalPosition]

GO

IF EXISTS (SELECT * FROM sys.indexes WHERE object_id = object_id('TradingData.OriginalPosition') and name = 'PK_OriginalPosition')
ALTER TABLE [TradingData].[OriginalPosition] DROP CONSTRAINT [PK_OriginalPosition]

GO

ALTER TABLE [TradingData].[OriginalPosition] ADD  CONSTRAINT [PK_OriginalPosition] PRIMARY KEY CLUSTERED 
(
	[FundBookEntityId] ASC,
	[SecurityId] ASC,
	[Date] ASC
)

IF EXISTS (SELECT * FROM sys.indexes WHERE object_id = object_id('TradingData.OriginalPosition') and name = 'IX_FundBookEntityId_Date')
DROP INDEX [IX_FundBookEntityId_Date] ON [TradingData].[OriginalPosition]

GO

CREATE NONCLUSTERED INDEX [IX_FundBookEntityId_Date] ON [TradingData].[OriginalPosition]
(
	[FundBookEntityId] ASC,
	[Date] ASC
)

GO


IF EXISTS (SELECT * FROM sys.indexes WHERE object_id = object_id('TradingData.OriginalPosition') and name = 'IX_FundBookEntityId_Date+SecurityId')
DROP INDEX [IX_FundBookEntityId_Date+SecurityId] ON [TradingData].[OriginalPosition]

GO

CREATE NONCLUSTERED INDEX [IX_FundBookEntityId_Date+SecurityId] ON [TradingData].[OriginalPosition]
(
	[FundBookEntityId] ASC,
	[Date] ASC
)
INCLUDE ( 	[SecurityId]) 

GO

IF EXISTS (SELECT * FROM sys.indexes WHERE object_id = object_id('TradingData.[OriginalPositionValidationData]') and name = 'IX_FundBookEntityId_Date')
DROP INDEX [IX_FundBookEntityId_Date] ON [TradingData].[OriginalPositionValidationData]

GO

CREATE NONCLUSTERED INDEX [IX_FundBookEntityId_Date] ON [TradingData].[OriginalPositionValidationData]
(
	[FundBookEntityId] ASC,
	[Date] ASC
)

